﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BYServer;

namespace ServerYuGi.Log
{
    public enum LogTag
    {
        BOBAI,
        CHIA_BAI,
        RUT_BAI,
        WIN,
        LOSE,
        SET_PLAYER_ID,
        ERROR_MSG,
        TURN_ID,
        RESPONSE_,
        API_RUTBAI,
        API_DISCARD
    }
    public class LogManager
    {
        string filename;       
        YServer serverSocket;
        StreamWriter wr;
        long NowTime;
        long BeginTime;

        public LogManager(YServer serverSocket)
        {
            this.serverSocket = serverSocket;
        }
        public void Create(int UserID1, int UserID2)
        {
            filename = "";
            filename += DateTime.Now.Year + "-";
            filename += DateTime.Now.Month + "-";
            filename += DateTime.Now.Day + "-";
            filename += DateTime.Now.Hour + "-";
            filename += DateTime.Now.Minute + "-";
            filename += UserID1 + "-";
            filename += UserID2;
            wr = new StreamWriter(serverSocket.Configuration.PATH_TXT_LOG + "\\" + filename + ".txt", true);
            GetTimeNow();
            BeginTime = NowTime;
        }
        public void Save()
        {          
            wr.Close();
        }

        void GetTimeNow()
        {
            NowTime = DateTime.Now.Ticks;
        }
        long CalculateTime()
        {
            long t;
            GetTimeNow();
            t =  NowTime - BeginTime;
            return t;
        }
       
        public void LogBoBai(string bobai)
        {
            wr.WriteLine(LogTag.BOBAI + " " + bobai);
        }
        public void LogChiaBai(int cardTypeID,int cardID,int playerID)
        {
            wr.WriteLine(LogTag.CHIA_BAI + " " + cardTypeID + " " + cardID + " " + playerID);
        }
        public void LogRutBai(int cardID,int playerID)
        {
            wr.WriteLine(LogTag.RUT_BAI + " " + cardID + " " + playerID);
        }
       
        public void LogWin(int playerID)
        {
            wr.WriteLine(LogTag.WIN + " " + playerID);
        }
        public void LogLose(int playerID)
        {
            wr.WriteLine(LogTag.LOSE + " " + playerID);
        }
     
        public void LogSetPlayer(int number, int playerID)
        {
            wr.WriteLine(LogTag.SET_PLAYER_ID + " " + number + " " + playerID);
        }      
        public void LogErrMsg(Common.YMSG msg,string des)
        {
            wr.WriteLine(LogTag.ERROR_MSG + " " + msg.ToString() + " " + des);
        }

        public void LogTurnID(int number)
        {
            wr.WriteLine(LogTag.TURN_ID + " " + number);
        }

        public void LogResponse(Common.YMSG msg,int playerID,object[] param)
        {
            string s = "";
            foreach (object i in param)
            {
                s += " " + i.ToString();
            }
            wr.WriteLine(playerID + " " + CalculateTime() +" " + LogTag.RESPONSE_ + msg + s);
        }

        public void LogAPIRutBai(int playerID, int number)
        {
            wr.WriteLine(LogTag.API_RUTBAI + " " + playerID + " " + number);
        }

        public void LogAPIDiscard(int playerID,int number)
        {
            wr.WriteLine(LogTag.API_DISCARD + " " + playerID + " " + number);
        }
        
    }
}
